package com.xizimall.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xizimall.mapper.TbContentCategoryMapper;
import com.xizimall.pojo.TbContentCategory;
import com.xizimall.pojo.TbContentCategoryExample;
import com.xizimall.pojo.TbContentCategoryExample.Criteria;
import com.xizimall.service.ContentCategoryService;
import com.xizimall.common.pojo.EUTreeNode;
import com.xizimall.common.pojo.XizimallResult;
/**
 * 内容分类服务
 * <p>Title: ContentCategoryServiceImpl</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月18日下午2:21:27
 * @version 1.0
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

	
	@Autowired 
	private TbContentCategoryMapper categoryMapper;
	
	@Override
	public List<EUTreeNode> getContentCategoryList(Long parentId) {
		//创建查询条件
		TbContentCategoryExample example=new TbContentCategoryExample();
		Criteria criteria=example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
	    List<TbContentCategory>list=categoryMapper.selectByExample(example);
		//将列表转换成TreeNodelist
	    List<EUTreeNode>resultList=new ArrayList<>();
	    for(TbContentCategory contentCategory:list){
	    	EUTreeNode treeNode=new EUTreeNode();
	        treeNode.setId(contentCategory.getId());
	        treeNode.setText(contentCategory.getName());
	        treeNode.setState(contentCategory.getIsParent()?"closed":"open");
	        resultList.add(treeNode);
	    }
	    return resultList;
	}

	@Override
	public XizimallResult insertContentCategory(Long parentId, String name) {
		//创建pojo
		TbContentCategory contentCategory=new TbContentCategory();
		contentCategory.setName(name);
		contentCategory.setIsParent(false);
		//状态1正常2删除
		contentCategory.setStatus(1);
		contentCategory.setParentId(parentId);
		contentCategory.setSortOrder(1);
		contentCategory.setCreated(new Date());
		contentCategory.setUpdated(new Date());
		//添加记录
		categoryMapper.insert(contentCategory);
		//查看父节点的isparent是否为true,如果不是,则改为true
		TbContentCategory parentCat=categoryMapper.selectByPrimaryKey(parentId);
		//判断
		if(!parentCat.getIsParent()){
			parentCat.setIsParent(true);
			//更新父节点
			categoryMapper.updateByPrimaryKey(parentCat);
		}
		return XizimallResult.ok();
	}

	@Override
	public XizimallResult deleteContentCategory(Long id) {
		TbContentCategory contentCategory=categoryMapper.selectByPrimaryKey(id);
		Long parentId=contentCategory.getParentId();
		categoryMapper.deleteByPrimaryKey(id);
		TbContentCategoryExample example=new TbContentCategoryExample();
		Criteria criteria=example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory>list=categoryMapper.selectByExample(example);
		if(list.size()==0){
			TbContentCategory parentCat=categoryMapper.selectByPrimaryKey(parentId);
		    parentCat.setIsParent(false);
		    categoryMapper.updateByPrimaryKey(parentCat);
		}
		return XizimallResult.ok();
	}

	@Override
	public XizimallResult updateContentCategory(Long id, String name) {
		TbContentCategory contentCategory=categoryMapper.selectByPrimaryKey(id);
		contentCategory.setName(name);
	    categoryMapper.updateByPrimaryKey(contentCategory);
		return XizimallResult.ok();
	}

}
